﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyncList.ValueObject;
using System.IO;
using System.Xml.Linq;

namespace SyncList.Persistance
{

    /// <summary>
    /// Importer les données
    /// </summary>
    class Import
    {

        /// <summary>
        /// Importer Xml
        /// </summary>
        /// <param name="fileName">Nom du fichier à importer</param>
        /// <returns></returns>
        public Data ImportXml(string fileName)
        {
            Data data = new Data();

            if (File.Exists(fileName))
            {
                XDocument xDoc = XDocument.Load(fileName);
                IEnumerable<FileDef> xFiles = from xFile in xDoc.Descendants("File")
                             select new FileDef
                             {
                                   Name = (string)xFile.Attribute("name"),
                                   Size = (long)xFile.Attribute("size"),
                                   Hash = (string)xFile.Attribute("hash"),
                                   LocalFolder = (string)xFile.Attribute("folder")
                             };

                foreach (FileDef file in xFiles)
                    data.Files.Add(file);

            }

            File.Copy(fileName, fileName + ".lastimport.bak", true);

            return (data);
        }


    }
}
